﻿#include "CameraFactory.h"
#include "CameraToupCam.h"

namespace Qly{

QList<IndustryCameraInfo> CameraFactory::availableCameras()
{
    QList<Qly::IndustryCameraInfo> list;
    ToupcamDeviceV2 arr[TOUPCAM_MAX];
    unsigned cnt = Toupcam_EnumV2(arr);
    if(cnt == 0) qDebug() << "no Touptek Camera found!";

    for(unsigned index = 0; index < cnt; index++)
    {
        Qly::IndustryCameraInfo info;
        info.type = Qly::CameraType::TOUP_USB;
        info.description = QString::fromWCharArray(arr[index].displayname);
        info.cameraID = QString::fromWCharArray(arr[index].id);
        info.modelName = QString::fromWCharArray(arr[index].model->name);

        list << info;
    }

    return list;
}

IndustryCamera *CameraFactory::findCameraByName(QString name, QObject *parent)
{
    Qly::CameraToupCam * p = new Qly::CameraToupCam(parent);
    if(!p->openCameraByID(name))
    {
        delete p;
        p =nullptr;
    }
    return p;
}

IndustryCamera *CameraFactory::defaultCamera(QObject *parent)
{
    Qly::CameraToupCam * p = new Qly::CameraToupCam(parent);
    if(!p->openDefaultCamera())
    {
        delete p;
        p =nullptr;
    }
    return p;
}

QString CameraFactory::vendorName()
{
    return QString("Touptek");
}


QList<IndustryCameraInfo> CameraFactory::availableCameras(QSet<CameraType> types)
{
    if( types.contains(CameraType::TOUP_USB) )
    {
        return availableCameras(CameraType::TOUP_USB);
    }
    return QList<IndustryCameraInfo>();
}

QList<IndustryCameraInfo> CameraFactory::availableCameras(CameraType type)
{
    QList<Qly::IndustryCameraInfo> list;
    if(type != CameraType::TOUP_USB)
    {
        return list;
    }
    ToupcamDeviceV2 arr[TOUPCAM_MAX];
    unsigned cnt = Toupcam_EnumV2(arr);
    if(cnt == 0) qDebug() << "no Touptek Camera found!";

    for(unsigned index = 0; index < cnt; index++)
    {
        Qly::IndustryCameraInfo info;
        info.type = Qly::CameraType::TOUP_USB;
        info.description = QString::fromWCharArray(arr[index].displayname);
        info.cameraID = QString::fromWCharArray(arr[index].id);
        info.modelName = QString::fromWCharArray(arr[index].model->name);

        list << info;
    }

    return list;
}
bool Qly::CameraFactory::release(IndustryCamera *camera)
{
    Qly::CameraToupCam * pCamera = dynamic_cast<Qly::CameraToupCam *> (camera); // 能够转换成功说明是这个dll new出来的对象。可以删除。
    if(pCamera)
    {
        delete pCamera;
        return true;
    }
    return false; //说明不能删除这个对象
}
} //namespace Qly

Qly::CameraFactoryInterface *CreateInstance()
{
    Qly::CameraFactory * cameraFactory = new Qly::CameraFactory();
    return cameraFactory;
}

void Release(Qly::CameraFactoryInterface *p)
{
    delete p;
}






